package com.github.soiff.shadow.plugin.helper;

import com.github.jknack.handlebars.Helper;
import com.github.jknack.handlebars.Options;

import java.io.IOException;

/**
 * Created by Soiff on 2016/05/20.
 *
 * @author : Soiff
 */
public class AddHelper implements Helper {
    @Override
    public CharSequence apply(Object source, Options options) throws IOException {
        Object param = options.param(0);
        if(source == null && param == null) {
            return "";
        } else if(source == null) {
            return param.toString();
        } else if(param == null) {
            return source.toString();
        } else if(source instanceof Double) {
            Double first2 = (Double)source;
            Double second2 = (Double)param;
            return String.valueOf(first2.doubleValue() + second2.doubleValue());
        } else {
            Integer first;
            Integer second;
            if(source instanceof Integer) {
                first = (Integer)source;
                second = (Integer)param;
                return String.valueOf(first.intValue() + second.intValue());
            } else if(source instanceof Long) {
                Long first1 = (Long)source;
                Long second1 = (Long)param;
                return String.valueOf(first1.longValue() + second1.longValue());
            } else if(source instanceof String) {
                first = Integer.valueOf(Integer.parseInt(source.toString()));
                second = Integer.valueOf(Integer.parseInt(param.toString()));
                return String.valueOf(first.intValue() + second.intValue());
            } else {
                throw new IllegalStateException("incorrect.type");
            }
        }
    }
}
